package com.example.demo;

import java.util.HashSet;
import java.util.Set;

/**
 * LCR 119. 最长连续序列
 * https://leetcode.cn/problems/WhsWhI/description/
 */
public class LongestConsecutive {

    public int longestConsecutive(int[] nums) {
        Set<Integer> set = new HashSet<>();
        for (int num : nums) {
            set.add(num);
        }

        int max = 0;

        for (int num : nums) {
            if (!set.contains(num - 1)) {
                int count = 1;
                int cur = num + 1;
                while (set.contains(cur)) {
                    count++;
                    cur++;
                }
                max = Math.max(max, count);
            }
        }

        return max;
    }
}
